//
//  UIViewController+FullScreenModal.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/1.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "UIViewController+FullScreenModal.h"

#import <objc/runtime.h> //添加头文件



@implementation UIViewController (FullScreenModal)

//因为是swizzing 所以需要在load方法中

+(void)load{
    [super load];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL originalSel = @selector(presentViewController:animated:completion:);
        SEL overrideSel = @selector(override_persentViewController:animated:completion:);
        
        Method originMet = class_getInstanceMethod(self.class, originalSel);
        Method overrideMet = class_getInstanceMethod(self.class, overrideSel);
        
        method_exchangeImplementations(originMet, overrideMet);
    });
}

#pragma mark - Swizzling
- (void)override_persentViewController:(UIViewController *)viewControllerTopresent animated:(BOOL) animated completion:(void (^ __nullable)(void))completion{
    //适配iOS13
    if(@available (iOS 13.0, *)){
        if(viewControllerTopresent.modalPresentationStyle == UIModalPresentationPageSheet){
            viewControllerTopresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    //根据title message 判断是否要弹出弹窗， 更改icon时防止弹出弹窗
    //如果是UIAlertController  需要进行判断
    if([viewControllerTopresent isKindOfClass:[UIAlertController class]]){
        //强转 为alerController 获得title 和 message
        UIAlertController *alerController = (UIAlertController *)viewControllerTopresent;
        if (alerController.title == nil && alerController.message == nil) {
            return;
        }else{
            [self override_persentViewController:viewControllerTopresent animated:animated completion:completion];
            return;
        }
    }
    [self override_persentViewController:viewControllerTopresent animated:animated completion:completion];
}

@end
